<template>
    <div>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()"><Icon type="ios-arrow-back" /> 返回  </Button>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">确认密接</div>
                <div class="line-main">
                    <Form ref="formCustomForClose" :model="formCustomForClose" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <FormItem  label="在管数量" prop="zg_num">
                                    <i-input type="number" number v-model="formCustomForClose.zg_num" placeholder="请输入在管数量">
                                        <span slot="append">人</span>
                                    </i-input>
                                </FormItem >
                            </Col>
                            <Col span="7">
                                <FormItem label="排除数量" prop="exclude_num">
                                    <i-input type="number" number v-model="formCustomForClose.exclude_num"  placeholder="请输入排除数量">
                                        <span slot="append">人</span>
                                    </i-input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="确诊数量" prop="confirm_num">
                                    <i-input type="number" number v-model="formCustomForClose.confirm_num"  placeholder="请输入确诊数量">
                                        <span slot="append">人</span>
                                    </i-input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1"  :value="formCustomForClose.imp_date" format="yyyy-MM-dd"
                                                type="date" placeholder="选择上报日期"></DatePicker>
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm(true,'formCustomForClose')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">疑似密接</div>
                <div class="line-main">
                    <Form ref="formCustomForSuspect" :model="formCustomForSuspect" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <Form-item label="在管数量" prop="zg_num">
                                    <i-input type="number" number v-model="formCustomForSuspect.zg_num">
                                        <span slot="append">人</span>
                                    </i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="排除数量" prop="exclude_num">
                                    <i-input type="number" number v-model="formCustomForSuspect.exclude_num">
                                        <span slot="append">人</span>
                                    </i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="确诊数量" prop="confirm_num">
                                    <i-input type="number" number v-model="formCustomForSuspect.confirm_num">
                                        <span slot="append">人</span>
                                    </i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1"  :value="formCustomForSuspect.imp_date"
                                                format="yyyy-MM-dd"
                                                type="date" placeholder="选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm(false,'formCustomForSuspect')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Modal v-model="modal" title="确认操作" width="20"  @on-ok="handleSubmit"     >
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>
    </div>
</template>

<script>
import { insert, findList, update } from '@/api/outbreak-management/key-population/epidemiological-Investigation'

export default {
  data () {
    const validateInt = (rule, value, callback) => {
      if (value % 1 !== 0) {
        callback(new Error('请输入整数'))
      } else {
        callback()
      }
    }
    return {
      options1: { // 不可选今天之后的日期
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      // 控制返回按钮显隐
      showBackBtn: false,
      // 弹窗控制
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 是否是确认密接
      isClose: false,
      formCustomForClose: {
        type: 0,
        zg_num: 0,
        exclude_num: 0,
        confirm_num: 0,
        imp_date: ''
      },
      formCustomForSuspect: {
        type: 1,
        zg_num: 0,
        exclude_num: 0,
        confirm_num: 0,
        imp_date: ''
      },
      ruleCustom: {
        zg_num: [
          { required: true, type: 'number', message: '在管人数必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '在管人数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        exclude_num: [
          { required: true, type: 'number', message: '排除人数必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '排除人数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        confirm_num: [
          { required: true, type: 'number', message: '确诊人数必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '确诊人数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        imp_date: [
          { required: true, message: '上报日期必填', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (isClose, name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.modal = true
          this.isClose = isClose
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },
    /**
             * 日期格式化
             */
    dateChange (date) {
      console.log(date)
      this.formCustomForClose.imp_date = date
      this.formCustomForSuspect.imp_date = date
    },
    handleSubmit () {
      let requestData = this.isClose ? this.formCustomForClose : this.formCustomForSuspect
      if (requestData.id) {
        update(requestData).then(({ data }) => {
          let { errcode, errmsg } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error(errmsg)
          }
        })
      } else {
        insert(requestData).then(({ data }) => {
          let { errcode, errmsg } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error(errmsg)
          }
        })
      }
    },
    isEditStatus () {
      if (this.$route.query && this.$route.query.imp_date) {
        console.log(this.$route.query.imp_date)
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.getEditData(this.$route.query.imp_date)
      }
    },
    getEditData (imp_date) {
      findList({ imp_date }).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          res.forEach(item => {
            if (item.type === '0') {
              this.formCustomForClose = item
            } else {
              this.formCustomForSuspect = item
            }
            let date = this.$common.moment(item.imp_date).format('YYYY-MM-DD')
            this.formCustomForSuspect.imp_date = date
            this.formCustomForClose.imp_date = date
          })
        }
      })
    }
  },
  created () {
    this.isEditStatus()
  },
  mounted () {
  }
}
</script>

<style scoped lang="less">

</style>
